const http = require('http')
const https = require('https')
const url = require('url')

http.createServer((req, res) => {
  const urlObj = url.parse(req.url)
  const options = {
    hostname: urlObj.hostname,
    path: urlObj.path,
    method: req.method,
    headers: req.headers
  }
  const request = https.request(options, response => {
    res.writeHead(response.statusCode, response.headers)
    response.pipe(res)
  })
  request.on('error', error => {
    console.error(error)
    res.end()
  })
  req.pipe(request)
}).listen(8080) // 代理服务器监听的端口

console.log('Proxy server running at http://localhost:8080/')